//
//  RegisterJustNeed.m
//  ZCJZ
//
//  Created by Angel_Yan on 2020/1/15.
//  Copyright © 2020 jeking. All rights reserved.
//

#import "RegisterJustNeed.h"
#import "SKBaseWebService.h"
#import "SKWebServiceErrorCode.h"
@interface RegisterJustNeed ()<UITextFieldDelegate>

@property (retain, nonatomic)UIImage *showimg;
@property (retain, nonatomic)UIImage *hiddenimg;


@property (assign , nonatomic) int secondLeft;
@property (retain , nonatomic) NSTimer *timer;


@property (weak, nonatomic) UITextField *unameTF;
@property (weak, nonatomic) UITextField *phoneTF;
@property (weak, nonatomic) UILabel *labCode;
@property (weak, nonatomic) UITextField *codeTF;
@property (weak, nonatomic) UITextField *upassTF;
@property (weak, nonatomic) UIImageView *imgVCShowPass;



@end

@implementation RegisterJustNeed

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    {
        self.navigationController.navigationBar.translucent = YES;
        [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init]
                                                      forBarMetrics:UIBarMetricsDefault];
        //去掉透明后导航栏下边的黑边
        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:UIColorFromRGB(0xffffff)};
        self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.unameTF becomeFirstResponder];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self.timer invalidate];
    self.timer = nil;
    [self.view endEditing:true];
 }


- (void)setupUI {

    _secondLeft = 0;
    
    self.timer = [NSTimer timerWithTimeInterval:1 repeats:true block:^(NSTimer * _Nonnull timer) {
        self.secondLeft -= 1;
    }];
    self.timer.fireDate = [NSDate distantFuture];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
    self.phoneTF.delegate = self;
    self.upassTF.secureTextEntry = YES;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //返回
    UIImage *closeimg = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e61e", 20, UIColorFromRGB(0x333333))];
    [self setLeftItemWithIcon:closeimg action:@selector(close4DismissViewController)];
    
    UIImage *showimg = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e646", 25, UIColorFromRGB(0x333333))];
    self.showimg = showimg;
    UIImage *hiddenimg = [UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e645", 25, UIColorFromRGB(0x333333))];
    self.hiddenimg = hiddenimg;

    self.imgVCShowPass.image = self.hiddenimg;
    self.imgVCShowPass.contentMode = UIViewContentModeCenter;
    
    self.labCode.text = @"发送验证码";
    self.labCode.textColor = UIColorFromRGB(0xF9150F);
}

- (void)close4DismissViewController {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [self.navigationController popViewControllerAnimated:true];
}

//MARK: - set
- (void)setSecondLeft:(int)secondLeft {
    _secondLeft = secondLeft;
    if(_secondLeft == 0) {
//        self.phoneTF.userInteractionEnabled = true;
        self.timer.fireDate = [NSDate distantFuture];
        self.labCode.text = @"发送验证码";
        self.labCode.textColor = UIColorFromRGB(0xF9150F);
    } else {
//        self.phoneTF.userInteractionEnabled = false;
        self.labCode.text = [NSString stringWithFormat:@"%d秒后重发",_secondLeft];
        self.labCode.textColor = UIColorFromRGB(0x999999);
    }
}

// MARK: - Action

// 获取验证码
- (void)btnCodeClick {
    if(self.secondLeft != 0) {
        return;
    }
    
    // 手机号
    NSString *phone = self.phoneTF.text;
    if(![generalToolBox isValidatePhoneNum:phone]) {
        [self.view makeToast:@"请输入正确的手机号" duration:2.0 position:CSToastPositionCenter];
        return;
    }
    
    //
    [self netSendSM];
}


// 密码隐显示
- (void)togleBtnAction {
    self.upassTF.secureTextEntry = !self.upassTF.secureTextEntry;
    if(self.upassTF.secureTextEntry) {
        self.imgVCShowPass.image = self.hiddenimg;
    } else {
        self.imgVCShowPass.image = self.showimg;
    }
}



// 审核
- (void)loginBtnClickAction {
    if (self.unameTF.text == nil || self.unameTF.text.length == 0) {
        [self.view makeToast:@"请输入账号" duration:2.0 position:CSToastPositionCenter];
        return;
    }
    
    NSString *phone = self.phoneTF.text;
    if (phone == nil || phone.length == 0) {
        [self.view makeToast:@"请输入手机号" duration:2.0 position:CSToastPositionCenter];
        return;
    }
    
    if(![generalToolBox isValidatePhoneNum:phone]) {
        [self.view makeToast:@"请输入正确的手机号" duration:2.0 position:CSToastPositionCenter];
        return;
    }
    
    if (self.codeTF.text == nil || self.codeTF.text.length == 0) {
        [self.view makeToast:@"请输入验证码" duration:2.0 position:CSToastPositionCenter];
        return;
    }
    
    if (self.upassTF.text == nil || self.upassTF.text.length == 0) {
        [self.view makeToast:@"请输入密码" duration:2.0 position:CSToastPositionCenter];
        return;
    }
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.view makeToast:@"提交成功，请耐心等待审核结果。" duration:2.0 position:CSToastPositionCenter];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:true];
        });
    });
}



// MARK: - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([string isEqualToString:@""]) {
        return YES;
    }
    if (textField.text.length >= 11) {
        return NO;
    }
    return YES;
}

// MARK: - NET
- (void)netSendSM {
    __weak typeof(self) weakSelf = self;
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:self.phoneTF.text forKey:@"phone"];
    [parameters setValue:@5 forKey:@"type"];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [SKBaseWebService getRequest:@"/v1/member/sms" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (status == kHXSNoError) {
            [weakSelf.view makeToast:@"已发送" duration:2.0 position:CSToastPositionCenter];
            weakSelf.secondLeft = 60;
            weakSelf.timer.fireDate = [NSDate date];
        } else {
            [weakSelf.view makeToast:msg duration:2.0 position:CSToastPositionCenter];
        }
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [weakSelf.view makeToast:msg duration:2.0 position:CSToastPositionCenter];
    }];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
